package Model;
import java.util.*;



public class SetSubTaskStatusCommand implements Command
{

@SuppressWarnings("unused")
private ComplexTask ParentTask;
private Task ChildChangedTask;
private ArrayList<Task> TasksPointer;
@SuppressWarnings("unused")
private ArrayList<Task> SubTasksPointer;
private boolean LastStatus;
private boolean CurrentStatus;
private boolean undone;


//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

	public SetSubTaskStatusCommand(ArrayList<Task> PointerToTasks)
	{
		TasksPointer=PointerToTasks;
        undone=false;
	}

	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

	public void execute(String taskName, boolean isTaskResolved)
	{

		for(int i = 0; i < TasksPointer.size(); ++i)
		{
			Iterator<Task> iteratesThroughParentTaskOfTree = TasksPointer.get(i).iterator();
			Iterator<Task> iteratesThroughParentTaskOfTree2 = TasksPointer.get(i).iterator(); //this iterator will always be 1-step behind.
			TaskTreeIterator temp;
			TaskTreeIterator temp2;
			temp = (TaskTreeIterator)iteratesThroughParentTaskOfTree;
			temp2 = (TaskTreeIterator)iteratesThroughParentTaskOfTree2;
			Task currentInTree=null;
			Task previousTaskInSearch=null;
			TasksPointer.get(i).mTodo.size();
			boolean laststepofDoWhile=false;
			boolean laststepofDoWhile2=true;
		
			do // go through entire Tree in search for ParentTask Name
			{
				if(laststepofDoWhile)
				{
					laststepofDoWhile2=false;
				}
				
				currentInTree = temp.getCurrent();

				System.out.println("-");
				System.out.println("Checking Element:" +currentInTree.getName());
									
				if(previousTaskInSearch == null && currentInTree.getName().equals(taskName))
				{
					currentInTree.setDone(isTaskResolved);
					return;
				}
				
				else if(currentInTree.getName().equals(taskName)) 
				{
					while(true)
					{
						Task loopTask = temp2.getCurrent();
						
						if(loopTask.mTodo.contains(currentInTree))
						{
							System.out.println("Changed Status...");
							ChildChangedTask = loopTask;
							currentInTree.setDone(isTaskResolved);
							return;
						}
						
						else
						{
							temp2.next();
						}
					}
				}
				
				if(previousTaskInSearch == null)
				{
					previousTaskInSearch = currentInTree;
				}

				if(temp.hasNext())
				{
					System.out.println("moving to next...");
					temp.next();

					if(temp.hasNext()==false)
					{
						laststepofDoWhile=true;
					}
				}
				
				else
				{
					laststepofDoWhile2=false;
				}
				
			}while(laststepofDoWhile2); //end of do-while-loop
		
		}//end of for-loop	
		

		//-------------------------------------------------------------
        //for(int i = 0; i < TasksPointer.size(); ++i)
		//{
        //    if(TasksPointer.get(i).getType().equals("Complex"))
        //    {
        //       ArrayList<Task> temp = TasksPointer.get(i).getSubtasks();
		//
		//
        //       for(int j = 0; j<temp.size() ; ++j )
        //       {
        //          if(temp.get(j).getName().equals(taskName))
        //          {
        //            ParentTask = (ComplexTask)TasksPointer.get(i);
        //            SubTasksPointer = temp;
        //            ChildChangedTask = temp.get(j);
        //            LastStatus = ChildChangedTask.getDone();
        //            CurrentStatus = isTaskResolved;
        //            ChildChangedTask.setDone(CurrentStatus);
        //            break;

        //          }
        //       }//end of inner foor loop
        //   }
		//}//end of outer for loop    
      //-------------------------------------------------------------
	}

	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

	public void undo()
	{
        if(ChildChangedTask != null && undone==false)
        {
            SetToLastStatus();
            undone=true;
        }
	}

	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

	public void redo()
	{
        if(ChildChangedTask !=null && undone)
        {
            SetToLastStatus();
            undone=false;
        }
	}

	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    private void SetToLastStatus()
    {
        if(ChildChangedTask != null)
        {
            ChildChangedTask.setDone(LastStatus);
            boolean temp;
            temp = LastStatus;
            LastStatus=CurrentStatus;
            CurrentStatus=temp;
        }

    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
}



